//
//  MyViewController.m
//  Hai
//
//  Created by 千金贷 on 16/2/23.
//  Copyright © 2016年 wangzijian. All rights reserved.
//

#import "StepAccountViewController.h"
#import "RegisterController.h"
#import "LoginController.h"
#import "PwdController.h"
#import "SJAvatarBrowser.h"
#import "UIImageView+WebCache.h"
#import "CaptureViewController.h"
#import "NickNameViewController.h"
#import "SignatureViewController.h"
#import "PhoneViewController.h"
#import "BoundPhoneController.h"
#import "BoundHusbandViewController.h"
#import "UpdatePwdByPwdController.h"
@interface StepAccountViewController ()<UIImagePickerControllerDelegate,PassImageDelegate>

@end

@implementation StepAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden =NO;
    self.navigationItem.title = @"帐户设置";
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewDidAppear:animated];
//    NSError *err;
//    NSData *jsonDataInfo = [[NSUserDefaults standardUserDefaults]objectForKey:@"memberinfo"];
//    NSDictionary *memberInfo = [NSJSONSerialization JSONObjectWithData:jsonDataInfo options:NSJSONReadingMutableContainers error:&err];
//    self.numlable.text =[NSString stringWithFormat:@"乐钓号：%@",GetmemberNum];
//    self.namelable.text =memberInfo[@"member_name"];
//    NSString *headurl =memberInfo[@"member_head"];
//    if (![headurl isEqual:[NSNull null]]) {
//        
//        //                    [_head setImage:[Util getImageFromURL:headurl]];
//        [_head sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseImageURL,headurl]]];
//        
//        _head.userInteractionEnabled = YES;
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage)];
//        [_head addGestureRecognizer:tap];
//        if (_head.image==nil) {
//            [_head setImage:[UIImage imageNamed:@"default_head"]];
//            
//        }
//        
//    }
    if (_head.image==nil) {
        [_head setImage:[UIImage imageNamed:@"head"]];
        
    }
}
-(void)showImage{
    if (_head.image!=nil) {
        [SJAvatarBrowser showImage:_head];//调用方法
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/**
 * 我的头像
 */
- (IBAction)myhead{
    [self choseButtonClicked];
};

/**
 * 我的昵称
 */
- (IBAction)mynickname{
    NickNameViewController *user = [[NickNameViewController alloc]init];
    [self.navigationController pushViewController:user animated:YES];
};

/**
 * 我的签名
 */
- (IBAction)mysignature{
    SignatureViewController *user = [[SignatureViewController alloc]init];
    [self.navigationController pushViewController:user animated:YES];
};

/**
 * 我的电话
 */
- (IBAction)myphone{
    int status = 1;
    if (status) {
        PhoneViewController *phone = [[PhoneViewController alloc]init];
        [self.navigationController pushViewController:phone animated:YES];

    }
    else{
        BoundPhoneController *bound = [[BoundPhoneController alloc]init];
        [self.navigationController pushViewController:bound animated:YES];

    }
};


- (IBAction)mytaskremind{};   //我的任务提醒

/**
 * 绑定老公
 */
- (IBAction)myhusband{
    BoundHusbandViewController *bound = [[BoundHusbandViewController alloc]init];
    [self.navigationController pushViewController:bound animated:YES];
};

/**
 * 修改密码
 */
- (IBAction)updatepwd{
    UpdatePwdByPwdController *bound = [[UpdatePwdByPwdController alloc]init];
    [self.navigationController pushViewController:bound animated:YES];
};


- (IBAction)switchaccounts{

}; //切换账号

-(void)passImage:(UIImage *)image{
   
    _head.image =image;
}

//弹出选项列表选择图片来源
- (void)choseButtonClicked{
    UIActionSheet *chooseImageSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从手机相册选择", nil];
    [chooseImageSheet showInView:self.view];
}

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    
}
#pragma mark UIActionSheetDelegate Method
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    
    switch (buttonIndex) {
        case 0://Take picture
            
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                
            }else{
                //NSLog(@"模拟器无法打开相机");
            }
            //            [self presentModalViewController:picker animated:YES];
            [self presentViewController:picker animated:YES completion:^(void){
                // Code
            }];
            
            break;
            
        case 1://From album
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            //            [self presentModalViewController:picker animated:YES];
            [self presentViewController:picker animated:YES completion:^(void){
                // Code
            }];
            break;
            
        default:
            
            break;
    }
}

#pragma 拍照选择照片协议方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    
    NSData *data;
    
    if ([mediaType isEqualToString:@"public.image"]){
        
        //切忌不可直接使用originImage，因为这是没有经过格式化的图片数据，可能会导致选择的图片颠倒或是失真等现象的发生，从UIImagePickerControllerOriginalImage中的Origin可以看出，很原始，哈哈
        UIImage *originImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        
        //图片压缩，因为原图都是很大的，不必要传原图
        UIImage *scaleImage = [self scaleImage:originImage toScale:0.3];
        
        //以下这两步都是比较耗时的操作，最好开一个HUD提示用户，这样体验会好些，不至于阻塞界面
        if (UIImagePNGRepresentation(scaleImage) == nil) {
            //将图片转换为JPG格式的二进制数据
            data = UIImageJPEGRepresentation(scaleImage, 1);
        } else {
            //将图片转换为PNG格式的二进制数据
            data = UIImagePNGRepresentation(scaleImage);
        }
        
        //将二进制数据生成UIImage`
        UIImage *image = [UIImage imageWithData:data];
        
        //将图片传递给截取界面进行截取并设置回调方法（协议）
        CaptureViewController *captureView = [[CaptureViewController alloc] init];
        captureView.delegate = self;
        captureView.image = image;
        //隐藏UIImagePickerController本身的导航栏
        //        picker.navigationBar.hidden = YES;
        captureView.type=1;
        [picker pushViewController:captureView animated:YES];
        
    }
}
#pragma mark- 缩放图片
-(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
    //    UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize));
    //    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height *scaleSize)];
    //    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    //    UIGraphicsEndImageContext();
    //    return scaledImage;
    
    CGSize size;
    if (image.size.height > 150) {
        
        size = CGSizeMake(150, 150);
    }else size = image.size;
    UIGraphicsBeginImageContext(size);
    CGRect rect = {{0,0}, size};
    [image drawInRect:rect];
    //    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    UIImage *compressedImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    return compressedImg;
    
}

@end
